import pytest
import requests,allure
class Api_base:
    def __init__(self,url):
        self.url=url
    @allure.step("发送POST请求:{path}")
    def post(self,path,**kwargs):
        url=f'{self.url}{path}'
        # 使用Allure步骤记录请求参数，在报告中显示为"请求参数{参数内容}"
        with allure.step(f'请求参数{kwargs}'):
            # 调用requests.post发送POST请求，传入完整URL和所有参数
            response=requests.post(url,**kwargs)
            # 将响应数据附加到Allure报告中
            allure.attach(
                response.text,
                name='响应数据',
                # Allure报告中用于指定附件类型的参数 以文本展示
                attachment_type=allure.attachment_type.TEXT
            )
            # 将响应状态码（如200、400）转换为字符串后附加到报告
            allure.attach(
                str(response.status_code),
                name='响应状态码',
                attachment_type=allure.attachment_type.TEXT
            )
        return response

    @allure.step("发送GET请求:{path}")
    def get(self, path, **kwargs):
        url = f'{self.url}{path}'
        with allure.step(f'请求参数{kwargs}'):
            response = requests.get(url,**kwargs)
            allure.attach(
                response.text,
                name='响应数据',
                attachment_type=allure.attachment_type.TEXT
            )
            allure.attach(
                str(response.status_code),
                name='响应状态码',
                attachment_type=allure.attachment_type.TEXT
            )
        return response

